package org.ar.gameserver.common.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CacheUtils {
	
	private static Connection conn;
	
	public static void createCache(String url, String name, String password) throws Exception {
		try {
			Class.forName("org.h2.Driver");  
			conn = DriverManager.getConnection(url, name, password); 
		} catch (Exception e) {
			LogUtils.error(e);
		}
	}
	
	public static void closeCacheConnection() throws SQLException {
		try {
			if(conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			throw e;
		}
	}

	public static synchronized ResultSet getCacheData(String sql) throws Exception {
		ResultSet rs = null;
		try {
			PreparedStatement ppst = conn.prepareStatement(sql);
			rs = ppst.executeQuery();
			
		} catch (Exception e) {
			throw e;
		}
		return rs;
	}
	
	public static synchronized int setCacheData(String sql) throws Exception {
		int result = 0;
		try {
			PreparedStatement ppst = conn.prepareStatement(sql);
			result = ppst.executeUpdate();
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
}
